<template>
  <div class="card-container">
    <el-form :model="form" :rules="formRules" ref="formRef" label-width="120px" style="width: 100%;" label-position="right">
      <el-card class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span>{{ form.pid == 0 ? '一' : '二' }}级权限</span>
        </div>
        <el-form-item label="权限名称：" prop="title">
          <el-input v-model="form.title"></el-input>
          <div class="tips">后台显示的权限名称</div>
        </el-form-item>
        <el-form-item label="排序：" prop="sort">
          <el-input v-model="form.sort"></el-input>
        </el-form-item>
        <el-form-item label="包含路由：" prop="route" v-if="form.pid == 0">
          <el-select v-model="level_one_route" placeholder="请选择">
            <el-option v-for="item in routeList" :key="item.id" :label="item.title" :value="item.id"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="包含路由：" prop="route" v-if="form.pid != 0">
          <el-cascader :options="routeList" :props="props" clearable @change="checkRoute"></el-cascader>
        </el-form-item>
      </el-card>
      <el-form-item>
        <el-button type="primary" @click="submitForm">立即添加</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getRouteLite, addPermission } from '@/api/permission'

export default {
  name: 'addPermission',
  data() {
    return {
      form: {
        title: '',
        pid: 0,
        sort: '1',
        content: [],
      },
      level_one_route: '',
      formRules: {
        title: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入权限排序', trigger: 'blur' }],
        content: [{ required: true, message: '请选择包含路由', trigger: 'blur' }],
      },
      routeList: [],
      props: { multiple: true, label: 'title', value: 'id', expandTrigger: 'hover' },
    }
  },
  created() {
    this.form.pid = this.$route.query.pid
    this.getRouteLite()
  },
  methods: {
    async getRouteLite() {
      const res = await getRouteLite()
      if (res.status != 1) return this.$message.error(res.msg)
      console.log(res)
      this.routeList = res.data
    },
    async submitForm() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return false
        if (this.form.pid == 0) {
          this.form.content = [this.level_one_route]
        }
        console.log(this.form)
        const res = await addPermission(this.form)
        if (res.status != 1) return this.$message.error(res.msg)
        this.$message.success(res.msg)
        this.closePage()
      })
    },
    checkRoute(val) {
      this.form.content = []
      for (var i = 0; i < val.length; i++) {
        this.form.content.push(val[i][1])
      }
      console.log(this.form.content)
    },
    resetForm() {
      this.$refs.formRef.resetFields()
      this.form = this.$options.data().form
    },
    async closePage() {
      let view = this.$route
      await this.$store.dispatch('tagsBar/delRoute', view)
      this.$router.push('/permissionList')
    },
  },
}
</script>

<style lang="scss" scoped>
// .el-form-item {
//   // width: 420px;
// }
::v-deep .el-input__inner {
  // margin-left: 20px;
  height: 36px;
  width: 420px;
}

.el-card {
  border-radius: 8px;
}
::v-deep .el-card__header {
  font-size: 14px;
  color: #555;
  font-weight: bold;
}
::v-deep .el-form-item__label {
  padding-right: 12px;
}
::v-deep .el-range-separator {
  line-height: 28px;
}
::v-deep .el-range-input {
  cursor: pointer;
}
.radio-box {
  display: flex;
  align-items: center;
  height: 36px;
  width: 600px;
}
.tips {
  color: #999;
  margin-top: 10px;
  font-size: 12px;
}
</style>
